<template>
<div>

<!--顶部面包屑导航  -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"></i>后台管理
    </el-breadcrumb-item>

    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
    <el-breadcrumb-item>第1步：选择商品类别</el-breadcrumb-item>

  </el-breadcrumb>
  <el-divider></el-divider>


<!-- 添加数据的表单  -->
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="商品类别" prop="categoryId">

      <el-cascader
          style="width: 100%;"
          placeholder="请选择父级类别，如不选择，则表示无父级类别"
          v-model="ruleForm.categoryId"
          :options="categoryOptions"></el-cascader>

    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>

  </el-form>

</div>
</template>




<script>
export default {
  data() {
    return {
      // 类别的下拉菜单的选项数据
      categoryOptions: [],
      // 表单
      ruleForm: {
        categoryId: '',
        categoryName: ''
      },
      // 表单规则
      rules: {
        categoryId: [
          {required: true, message: '请选择商品类别', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {

    //加载本地存储的表单数据
    loadLocalRuleForm(){
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);
      if (localRuleForm && localRuleForm.categoryId){
        this.ruleForm  = localRuleForm;
      }
    },



    // 提交表单：到下一步
    submitForm(formName) {
      if (Array.isArray(this.ruleForm.categoryId)) {
        this.ruleForm.categoryId = this.ruleForm.categoryId.pop();
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发请求，查询类别详情，判断类别是否存在，类别是否包含子级类别，如果均没有问题，则跳转到下一步
          let url = 'http://localhost:9180/category/list';
          console.log('url = ' + url);


          let localJwt = localStorage.getItem('localJwt')

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .get(url).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              let category = jsonResult.data;
              if (category.isParent == 1) {
                let message = '错误：选择的类别必须不包含子级类别！';
                this.$alert(message, '警告', {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
              } else {
                this.ruleForm.categoryName = category.name;
                localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
                this.$router.push('add-new-2');
              }
            } else {
              this.$alert(jsonResult.message, '警告', {
                confirmButtonText: '确定',
                callback: action => {
                  this.loadAlbumList();
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },




    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },


    // 加载类别树的数据
    loadCategoryTree() {
      let url = 'http://localhost:9180/category/tree';
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt')
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.categoryOptions = jsonResult.data;
          this.ruleForm.parentId = "0";
        }
      });
    }


      },
    mounted(){
      this.loadLocalRuleForm();
      this.loadCategoryTree();
    }

}
</script>

